<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

$THEME->addBreadcrumb($this->adminUrl.'/workspaces/assign', $this->lang('ap_title_workspaces_assign'));

$workspaceid = (int) get('workspace');

$workspaces = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}workspaces ORDER BY title ASC");
$wsOpts = array('0' => 'Select a Workspace');
foreach($workspaces as $k=>$v){
    $wsOpts[$v['id']] = $v['title'];
};

$wsSelect = '<select id="jfx-workspace-select">';
$wsOptString = '';
foreach($wsOpts as $k=>$v){
    if($k==$workspaceid) $selected = 'selected="selected"';
    else $selected = '';
    $wsSelect .= '<option value="'.$k.'" '.$selected.'>'.$v.'</option>';
}
$wsSelect .= '</select>';


$VIEW->addJs('
$(document).ready(function(){
    $("#jfx-workspace-select").change(function(){
        window.location = "'.$CONFIG->adminUrl.'/workspaces/assign?workspace="+this.options[this.selectedIndex].value;
    });
});
');

echo $wsSelect;


if(get('workspace')==''){
    // need to select a workspace
    
}else{
    // have selected a workspace
    $workspaceid = (int) get('workspace');
    if($DB->countRows($CONFIG->dbprefix.'workspaces', "id = '{$workspaceid}'")==0){
        JFX::redirect($CONFIG->adminUrl.'/workspaces/assign');
    };

    // we have a valid workspace id!
    $allUsers = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}admin_users ORDER BY username ASC");
    
    if(Antz_IntelliForm::submitted('assign-to-workspace')){
        foreach($allUsers as $k=>$v){
            if(post('user_'.$v['id'])==1){
                if($DB->countRows($CONFIG->dbprefix.'admin_users_to_workspaces', "user_id = '{$v['id']}' AND workspace_id = '{$workspaceid}'")==0){
                    $dbParams = array(
                        'user_id' => $v['id'],
                        'workspace_id' => $workspaceid
                    );
                    $DB->insert($CONFIG->dbprefix.'admin_users_to_workspaces', $dbParams);
                }
            }else{
                $DB->delete($CONFIG->dbprefix.'admin_users_to_workspaces', "user_id = '{$v['id']}' AND workspace_id = '{$workspaceid}'");
            }
        }
        JFX::addSuccess('Updated workspace assignments');
        JFX::redirect(THIS_PAGE_URL);
    }


    foreach($allUsers as $k=>$v){
        $assigned = (bool) $DB->countRows($CONFIG->dbprefix.'admin_users_to_workspaces', "workspace_id = '{$workspaceid}' AND user_id = '{$v['id']}'");
        if($assigned) $checked = 'checked="checked"';
        else $checked = '';

        $allUsers[$k]['checkbox'] = '<input type="checkbox" name="user_'.$v['id'].'" value="1" '.$checked.' />';

    }

    $SMARTY->assign('workspaceTitle', $DB->oneValue($CONFIG->dbprefix.'workspaces', 'title', "id = '{$workspaceid}'"));
    $SMARTY->assign('users', $allUsers);
    $SMARTY->assign('AssignAntzIntelliFormSeed', Antz_IntelliForm::seed('assign-to-workspace'));
    echo $SMARTY->fetch('admin/workspaces/assign.tpl');

}

?><div class="jfx-clear"></div>
